package com.ddf.publics.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ddf.norman.util.DateUtil;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: VerificationCodeUtil.java
 * @Description: 发送和查询验证码
 * @author: norman
 * @date:  2020-05-06 15:03
 * @version V1.0
 */
@Component
public class VerificationCodeUtil {

    @Value("${aliyun.accessKeyId}")
    public String accessKeyId;
    @Value("${aliyun.AccessKeySecret}")
    public String accessKeySecret;

    public static final String ALI_TEMPLATE_REGISTER = "SMS_193236954";//注册
    public static final String ALI_TEMPLATE_FORGOT = "SMS_193236955";//找回密码
    public static final String ALI_TEMPLATE_BINDING_BANK = "SMS_193238923";//绑定银行卡
    public static final String ALI_TEMPLATE_DE_BINDING_BANK = "SMS_195221356";//解绑银行卡
    public static final String ALI_TEMPLATE_CHANGE_PHONE = "SMS_193248842";//更换手机号
    public static final String ALI_TEMPLATE_QQ_BINDING = "SMS_195261294";//qq绑定叮叮金融用户

    public String sendCode(String phone,String templateCode, String code){
        System.out.println("phone = " + phone);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "叮叮");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(response.getData());
        Object message = jsonObject.get("Message");
        if (message != null) {
            return message.toString();
        }
        return "error";
    }
    public boolean queryCode(String phone, String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySendDetails");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumber", "15717973152");
        request.putQueryParameter("SendDate", DateUtil.format(new Date(),"yyyyMMdd"));
        request.putQueryParameter("PageSize", "10");
        request.putQueryParameter("CurrentPage", "1");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }
}
